'use strict';

// 创建交易记录
function createRecord(tradeObjects, $parent) {
    var typeName = {
        "D00001": {
            "name": "充值",
            "png": "jiesuan_icon"
        },
        "D00002": {
            "name": "提现",
            "png": "quxian_icon"
        },
        "D00004": {
            "name": "结算",
            "png": "jiesuan_icon"
        },
        "D00005": {
            "name": "退货",
            "png": "tuihuo_icon"
        },
        "E00001": {
            "name": "自动结算-充值",
            "png": "jiesuan_icon"
        },"E00002": {
            "name": "自动结算-取现",
            "png": "jiesuan_icon"
        }
    };
    for (var i = 0, l = tradeObjects.length; i < l; i++) {

        var tradeObject = tradeObjects[i];
        tradeObject.name = typeName[tradeObject.trans_type].name;
        tradeObject.pngName = typeName[tradeObject.trans_type].png;
        tradeObject.formatedDate = changeDateTime(tradeObject.trans_date, "second");
        var template = `
            <div class="weui-cell">
                <div class="weui-cell__hd"><img class="icon" src="../images/tltd/${tradeObject.pngName}.png"></div>
                <div class="weui-cell__bd">
                    <p class="trade_name">${tradeObject.name}</p>
                    <p class="trade_time">${tradeObject.formatedDate}</p>
                </div>
                <div class="weui-cell__ft">
                    <p class="trade_money">${tradeObject.trans_amt}元</p>
                    <p class="trade_type">成功</p>
                </div>
            </div>`;

        $parent.append(template);
    }

}

// 显示loading
function showLoadingDialog() {
    $("#loadingDialog").css({
        "opacity": "1",
        "display": ""
    });
}

// 隐藏loading
function hideLoadingDialog() {
    $("#loadingDialog").fadeOut(200);
}



// 时间20161108160023 转为 2016-11-08 16:00:23
function changeDateTime(dateString, type) {
    var pattern = /(\d{4})(\d{2})(\d{2})/;
    dateString = String(dateString);
    if (dateString.length < 14) { // 没有时分秒的补全
        dateString += "000000";
    }
    var formatedDate;
    if (type == "time") {
        pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})/;
        formatedDate = dateString.substr(0, 12).replace(pattern, '$1-$2-$3 $4:$5');
    } else if (type == "second") {
        pattern = /(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/;
        formatedDate = dateString.substr(0, 16).replace(pattern, '$1-$2-$3 $4:$5:$6');
    } else if (type == "date") {
        formatedDate = dateString.substr(0, 8).replace(pattern, '$1-$2-$3');
    }

    return formatedDate;
}


export {
    createRecord,
    showLoadingDialog,
    hideLoadingDialog
}